<?php
namespace app\common\model\mall;
use app\common\model\TimeModel;

class Store extends TimeModel
{
    /**
     *机构配置 图片
     */
    public function getImageAttr($value){
        if(!empty($value)){
            return explode(',',$value);
        }
    }

    public function setImageAttr($value){
        if(!empty($value)){
            return implode(',',$value);
        }
    }
    /**
     *机构配置 服务电话
     */
    public function getServicePhoneAttr($value){
        if(!empty($value)){
            return explode(',',$value);
        }
    }

    /**
     * 获取教师
     */
    public function teacher()
    {
        return $this->hasMany('app\common\model\business\OrganTeacher','store_id','id');
    }

    /**
     * 关联标签
     */
    public function Tag(){
        return $this->belongsTo('app\common\model\common\RelationTag','id','other_id')->field('id,other_id,tag_id');
    }

    /**
     * 关联商家资料表
     */
    public function StoreData(){
        return $this->hasOne('app\common\model\mall\StoreData','store_id','id');
    }

    /**
     * 关联商家地址表
     */
    public function StoreAddress(){
        return $this->hasMany('app\common\model\mall\StoreAddress','store_id','id');
    }

    /**
     * 关联标签
     */
    public function Tags(){

        return $this->belongsToMany('app\common\model\common\Tags','relation_tag','tag_id','other_id')->where(['pivot.type'=>1,'pivot.delete_time'=>Null]);

    }
    public static function onAfterUpdate($data){

        $store_tag = app('app\common\model\common\RelationTag');

        if(!empty($data->tags) || !empty($data->store_tag)){//修改操作

               if(explode(',',$data->store_tag)!=$data->tags){
                   $where = [];
                   if(!empty($data->tags)){
                       $where[] = ['id','notin',$data->tags];
                   }
                   $a = $store_tag->where([['other_id','=',$data->id]])->where($where)->select();
                   $a->delete();
                   $arr = [];
                   $tag = $store_tag->where([['other_id','=',$data->id]])->column('id');

                   if(!empty($data->tags)){

                       foreach($data->tags as $key=>$value){
                           if(!in_array($value,$tag)){
                               $arr[$key] = ['other_id'=>$data->id,'tag_id'=>$value,'type'=>1];
                           }
                       }
                       $store_tag->saveAll($arr);
                   }
               }
        }
    }

    public static function onAfterInsert($data){
        $store_tag = app('app\common\model\common\RelationTag');
        $StoreUser = app('app\common\model\system\SystemAdmin');
            $arr = [];
            foreach($data->tags as $key=>$value){
                $arr[$key] = ['other_id'=>$data->id,'tag_id'=>$value,'type'=>1];
            }
            $store_tag->saveAll($arr);
        //新增默认商家超级用户
        $StoreUser->save(['nickname'=>$data['name'].'超级管理员','auth_ids'=>2,'app_id'=>self::$app_id,
            'phone'=>$data['phone'],'username'=>$data['phone'],'password'=>password($data['phone']),'store_id'=>$data->id,'is_super'=>0
        ]);
    }
    public static function onAfterDelete($data){
        $store_tag = app('app\common\model\common\RelationTag');
        $admin = app('app\common\model\system\SystemAdmin');

        $store = $store_tag::where('other_id',$data->id)->select();
        $store->delete();
        $admin = $admin::where('store_id',$data->id)->select();
        $admin->delete();
    }
}